package com.ljh.controller;


import com.ljh.VO.BarVo;
import com.ljh.VO.DataVo;
import com.ljh.VO.PieVo;
import com.ljh.entity.Fruit;
import com.ljh.service.FruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李佳豪
 * @since 2021-08-16
 */
@RestController
@RequestMapping("//fruit")
public class FruitController {
    @Autowired
    private FruitService fruitService;
    @GetMapping
    public List<Fruit> listAll(){
        return fruitService.list();
    }
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable("id") Integer id){
        return fruitService.removeById(id);
    }
    @GetMapping("/find/{id}")
    public Fruit find(@PathVariable("id") Integer id){
        return fruitService.getById(id);
    }
    @PutMapping("/update")
    public Boolean update(@RequestBody Fruit fruit){
        return fruitService.updateById(fruit);
    }
    @PostMapping("/add")
    public boolean add(@RequestBody Fruit fruit){
        return fruitService.save(fruit);
    }
    @GetMapping("/barvo")
    public BarVo Barvo(){
        BarVo barVo = new BarVo();
        List<String> names=new ArrayList();
        List<DataVo> values=new ArrayList();

        List<Fruit> list = fruitService.list();
        for (Fruit fruit : list) {
            names.add(fruit.getName());
            DataVo dataVo = new DataVo();
            dataVo.setValue(fruit.getSale());
            HashMap<String, String> map = new HashMap<>();
            map.put("color","#3fb1e3");
            dataVo.setItemStyle(map);
            values.add(dataVo);
        }
        barVo.setNames(names);
        barVo.setDataVos(values);
        return barVo;
    }
    @GetMapping("/pievo")
    public List<PieVo> pieVo(){
        ArrayList<PieVo> list1 = new ArrayList<>();
        List<Fruit> list = fruitService.list();
        for (Fruit fruit : list) {
            PieVo pieVo = new PieVo();
            pieVo.setValue(fruit.getSale());
            pieVo.setName(fruit.getName());
            list1.add(pieVo);
        }
        return list1;
    }

}

